﻿using DataCenter.Model.Interface;
using DataCenter.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common.Util;

namespace DataCenter.Provider
{
    public class ModelCatProvider : GenericProvider, IModelCatProvider
    {
        public List<ModelCat> SearchModelCats(out int totalrecord, string model = null, int pagesize = 0, int pageindex = 0)
        {
            var querry = repository.GetQuery<ModelCat>();
            if (!model.IsNullOrWhiteSpace())
            {
                var modelCheck = model.Trim().ToUpper();
                querry = querry.Where(x => x.Model.ToUpper().Contains(modelCheck));
            }
            totalrecord = querry.Count();
            if (pagesize > 0 && pageindex >= 0)
            {
                querry = querry.OrderBy(x => x.ProdL1).ThenByDescending(x => x.CreateDate);
                querry = querry.Skip(pageindex * pagesize).Take(pagesize);
            }
            return querry.ToList();
        }


        public ModelCat RetrieveModel(string model)
        {
            if (!model.IsNullOrWhiteSpace())
            {
                var id = model.ToUpper();
                var querry = repository.GetQuery<ModelCat>(x => x.Model.ToUpper() == id);
                return querry.FirstOrDefault();
            }
            return null;
        }

        public bool Add(ModelCat obj)
        {
            try
            {
                repository.Add<ModelCat>(obj);
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Update(ModelCat obj)
        {
            try
            {
                if (obj.Model.IsNullOrWhiteSpace()) return false;
                var model = obj.Model.ToUpper();
                var updateRecord = repository.FindOne<ModelCat>(x => x.Model.ToUpper() == model);
                if (updateRecord == null) return false;
                updateRecord. ActiveCode = obj.ActiveCode;
                updateRecord.AfCode = obj.AfCode;
                updateRecord.AuCode = obj.AuCode;
                updateRecord.CreateDate = obj.CreateDate;
                updateRecord.EnableFlag = obj.EnableFlag;
                updateRecord.Eom = obj.Eom;
                updateRecord.IfDate = obj.IfDate;
                updateRecord.LastUpdateDate = obj.LastUpdateDate;
                updateRecord.Mkt = obj.Mkt;
                updateRecord.Model = obj.Model;
                updateRecord.ModelSpec = obj.ModelSpec;
                updateRecord.MpModel = obj.ModelSpec;
                updateRecord.MpSuffix = obj.MpSuffix;
                updateRecord.ProdL1 = obj.ProdL1;
                updateRecord.ProdL2 = obj.ProdL2;
                updateRecord.ProdL3 = obj.ProdL3;
                updateRecord.ProdL4 = obj.ProdL4;
                updateRecord.ProdType = obj.ProdType;
                updateRecord.PurType = obj.PurType;
                updateRecord.SaleFlag = obj.SaleFlag;
                updateRecord.SoTransferDate = obj.SoTransferDate;
                updateRecord.SoTransferFlag = obj.SoTransferFlag;
                updateRecord.Suffix = obj.Suffix;
                updateRecord.TentativeFlag = obj.TentativeFlag;
                updateRecord.Unit = obj.Unit;
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
